package com.kingint.web.admin.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kingint.common.unitedresult.K;
import com.kingint.mbg.bas.entity.BasServiceClass;
import com.kingint.mbg.bas.mapper.BasServiceClassMapper;
import com.kingint.mbg.sys.entity.UmsMember;
import com.kingint.web.admin.base.service.BasServiceClassService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kingint.web.sys.service.UmsAdminService;
import com.kingint.web.util.MenuUtil;
import com.kingint.web.util.tree.TreeBo;
import com.kingint.web.util.tree.TreeUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务内容表（服务工单中的服务内容） 服务实现类
 * </p>
 *
 * @author wxh
 * @since 2020-12-21
 */
@Service
@Slf4j
public class BasServiceClassServiceImpl extends ServiceImpl<BasServiceClassMapper, BasServiceClass> implements BasServiceClassService {

    @Resource
    private BasServiceClassMapper serviceClassMapper;
    @Resource
    private UmsAdminService adminService;

    @Override
    public int insertServiceClassInfo(BasServiceClass basServiceClass) {
        UmsMember currentMember = adminService.getCurrentMember();
        Integer hotelId = currentMember.getHotelId();
        if (StringUtils.isEmpty(hotelId)) {
            log.debug("添加服务内容对应的账号没有关联酒店id");
            return 0;
        }
        basServiceClass.setCreateTime(new Date());
        basServiceClass.setHotelId(hotelId);
        int insert = serviceClassMapper.insert(basServiceClass);
        return insert;
    }

    @Override
    public Object getBasServiceList(Integer pageNum, Integer pageSize) {
        UmsMember currentMember = adminService.getCurrentMember();
        Integer hotelId = currentMember.getHotelId();
        if (StringUtils.isEmpty(hotelId)) {
            log.debug("查询服务内容对应的账号没有关联酒店id");
            return null;
        }
//        IPage<BasServiceClass> page = new Page<>(pageNum, pageSize);
//        //查询一级分类 服务，物品
//        QueryWrapper<BasServiceClass> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("hotel_id", hotelId)
//                .eq("pid", 0)
//                .eq("status", 0);
//        IPage<BasServiceClass> page1 = serviceClassMapper.selectPage(page, queryWrapper);
//        //查询所有的信息
//        QueryWrapper<BasServiceClass> wrapper = new QueryWrapper<>();
//        wrapper.eq("hotel_id", hotelId)
//                .eq("status", 0);
//        List<BasServiceClass> list = serviceClassMapper.selectList(wrapper);
//        MenuUtil.cancelServiceClass(page1.getRecords(), list);
//        return page1;
//    }
        QueryWrapper<BasServiceClass> wrapper = new QueryWrapper<>();
        wrapper.eq("hotel_id", hotelId)
                .eq("status", 0);
        List<BasServiceClass> list = this.list(wrapper);
        TreeUtil<BasServiceClass> basGoodsTreeUtil = new TreeUtil<BasServiceClass>("getId","getPid");

        List<TreeBo<BasServiceClass>> createTreeAsTreeEntity=null;
        List<BasServiceClass> list2=new ArrayList<>();
        try {

            createTreeAsTreeEntity = basGoodsTreeUtil.createTreeAsTreeEntity(new HashSet<BasServiceClass>(list));
            if(createTreeAsTreeEntity.size()>0)
                tree(list2,createTreeAsTreeEntity);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return K.ok().data(list2);
    }
    private void  tree(List<BasServiceClass> list,List<TreeBo<BasServiceClass>>  TreeBo){
        for (TreeBo<BasServiceClass> itemsTreeBo : TreeBo) {
            BasServiceClass title = itemsTreeBo.getTitle();
            List<TreeBo<BasServiceClass>> children = itemsTreeBo.getChildren();
            if(children!=null){
                tree(title.getChildren(),children);
            }
            list.add(title);
        }

    }

    @Override
    public int deleteServiceClassInfo(Integer id) {
        BasServiceClass aClass = new BasServiceClass();
        aClass.setId(id);
        aClass.setStatus(1);
        int i = serviceClassMapper.updateById(aClass);
        return i;
    }

    @Override
    public int updateServiceInfo(BasServiceClass basServiceClass) {
        int i = serviceClassMapper.updateById(basServiceClass);
        return i;
    }

    @Override
    public List<BasServiceClass> getAllServiceClassList() {
        UmsMember currentMember = adminService.getCurrentMember1();
        Integer hotelId = currentMember.getHotelId();
        if (StringUtils.isEmpty(hotelId)) {
            log.debug("查询服务内容对应的账号没有关联酒店id");
            return null;
        }
        QueryWrapper<BasServiceClass> wrapper = new QueryWrapper<>();
        wrapper.eq("hotel_id", hotelId)
                .eq("status", 0);
        List<BasServiceClass> list = serviceClassMapper.selectList(wrapper);
        if (!CollectionUtils.isEmpty(list)) {
            return handleServiceClass(list);
        }
        return null;
    }

    private List<BasServiceClass> handleServiceClass(List<BasServiceClass> list) {
        List<BasServiceClass> newList = new ArrayList<>();
        List<BasServiceClass> collect = list.stream().filter(s -> s.getPid() == 0).collect(Collectors.toList());
        if (collect.size() != 0) {
            for (BasServiceClass info : collect) {
                String ls = info.getName();
                handleInfo(list, info, ls, newList);
            }
        }
        return newList;
    }

    private void handleInfo(List<BasServiceClass> collect, BasServiceClass info, String ls, List<BasServiceClass> list) {
        List<BasServiceClass> classes = collect.stream().filter(s -> s.getPid().equals(info.getId())).collect(Collectors.toList());
        if (classes.size() != 0) {
            for (BasServiceClass info1 : classes) {
                String name = ls + ";" + info1.getName();
                handleInfo(collect, info1, name, list);
            }
        } else {
            Integer isSort = info.getIsSort();
            if (isSort == 0) {//物品
                BasServiceClass aClass = new BasServiceClass();
                aClass.setName(ls);
                aClass.setId(info.getId());
                list.add(aClass);
            }
        }
    }
}
